package com.tiny.persistence.repository.impl;

import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.apache.log4j.Logger;

import com.tiny.persistence.domain.BgMenu;
import com.tiny.persistence.repository.plus.BgMenuRepoPlus;
/**
 *后台菜单仓储层
* @ClassName:  
* @Description: TODO 
* @author:  
* @date: 2014年3月23日
*
 */
public class BgMenuRepoImpl extends BaseRepoImpl<BgMenu> implements BgMenuRepoPlus{

    private static Logger logger = Logger.getLogger(BgMenuRepoImpl.class);

    @Override
    public List<BgMenu> getListByCondition(Map<String, Object> condition) {
        Query query = em.createQuery("from BgMenu as re where as.isShow=1");
        
        //query.setFirstResult(arg0);
        //query.setMaxResults(arg0);
        
        @SuppressWarnings("unchecked")
		List<BgMenu> list = query.getResultList();
        
        logger.info("======= " + list);
        
        return list;
    }

    @Override
    public int getCountByCondition(Map<String, Object> condition) {
        
        Query query = em.createQuery("select count(*) from BgMenu as au where as.isShow=1");
        Object obj = query.getSingleResult();
        
        logger.info("======= count=" + obj);
        
        return Integer.parseInt(obj.toString());
    }

    public List<BgMenu> findFirstLevelMenuList(int bgRoleId) {
    
    	String hql = "from BgMenu o where o.isShow=1 and o.parentId = -1 and o.id in(select bgMenuId from BgRoleMenu where bgRoleId = "+bgRoleId+")";
        Query query = em.createQuery(hql);
        
        @SuppressWarnings("unchecked")
		List<BgMenu> list = query.getResultList();
        
        logger.info("======= " + list);
        
        return list;
    }

    public List<BgMenu> findSecondLevelMenuListByMenuId(int menuId, int bgRoleId) {
        
        Query query = em.createQuery("from BgMenu o where o.isShow=1 and o.parentId = "+menuId+" and o.id in(select bgMenuId from BgRoleMenu where bgRoleId = "+bgRoleId+")");
        
        @SuppressWarnings("unchecked")
		List<BgMenu> list = query.getResultList();
        
        logger.info("======= " + list);
        
        return list;
    }

	public List<BgMenu> findAllMenuList(int bgRoleId) {
		String hql = "from BgMenu o  where o.isShow=1 and o.id in(select bgMenuId from BgRoleMenu where bgRoleId = "+bgRoleId+")";
        Query query = em.createQuery(hql);
        
        @SuppressWarnings("unchecked")
		List<BgMenu> list = query.getResultList();
        
        logger.info("======= " + list);
        
        return list;
	}


}
